package com.xjcs.saplingplan.ui.adapter.countdown

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.xjcs.saplingplan.R
import com.xjcs.saplingplan.const.CountdownRepeatType
import com.xjcs.saplingplan.const.setSingleClick
import com.xjcs.saplingplan.databinding.ItemCountdownRepeatBinding
import kotlin.enums.EnumEntries

class CountdownRepeatAdapter(
    private val context: Context,
    private val list: EnumEntries<CountdownRepeatType>,
    private val itemClick: (type: CountdownRepeatType) -> Unit
) : RecyclerView.Adapter<ItemCountdownRepeatViewHolder>() {

    private var selectPos = 0

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ItemCountdownRepeatViewHolder {
        val binding =
            ItemCountdownRepeatBinding.inflate(LayoutInflater.from(context), parent, false)
        return ItemCountdownRepeatViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: ItemCountdownRepeatViewHolder, position: Int) {
        val item = list[position]
        if (selectPos == holder.adapterPosition) {
            holder.binding.tvText.setBgColor(ContextCompat.getColor(context, R.color.color_34C759))
            holder.binding.tvText.setTextColor(Color.WHITE)
        } else {
            holder.binding.tvText.setBgColor(Color.WHITE)
            holder.binding.tvText.setTextColor(Color.BLACK)
        }
        holder.binding.tvText.text = item.desc
        holder.binding.tvText.setSingleClick {
            selectPos = holder.adapterPosition
            notifyDataSetChanged()
            itemClick.invoke(item)
        }
    }
}

class ItemCountdownRepeatViewHolder(val binding: ItemCountdownRepeatBinding) :
    ViewHolder(binding.root)